<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['types']=array(
	'1'=>'普通订阅号',
	'2'=>'认证订阅号',
	'3'=>'普通服务号',
	'4'=>'认证服务号'
);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['merchname'])){
		$params['merchname like']='%'.trim($_RQ['merchname']).'%';
		$return['params']['merchname']=$_RQ['merchname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['status']!=''){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['pcd']){
		$pcd=cfc('tools')->base64_decode($_RQ['pcd'],$_SESSION['base_key_str']);
		$pcd=json_decode($pcd,true);
		$return['params']['pcd']=$pcd;
	}
	if($pcd['province']){
		$params['pcd like']='%'.$pcd['province'].'%';
	}
	if($pcd['city']){
		$params['pcd like']='%'.$pcd['city'].'%';
	}
	if($pcd['district']){
		$params['pcd like']='%'.$pcd['district'].'%';
	}
	if($pcd['street']){
		$params['pcd like']='%'.$pcd['street'].'%';
	}
	$fields=array('id','mid','star','groupid','isrecommand','salecate','cateid','mobile','realname','status','jointime','accounttime','logo','merchname');
    $orderby='star desc, id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_user',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as &$v){
		$v['jointime']=date('Y/m/d H:i:s',$v['jointime']);
		$v['member']=cfc('core')->member_show($v['mid']);
		$v['appurl']='jy_merch/merch//id='.$v['id'];
		$v['catename']=pdo_getcolumn('jy_merch_category',array('id'=>$v['cateid'],'uniacid'=>$_SESSION['uniacid']),'catename');
		$v['catename']=$v['catename']?:'';
		$v['membernum']=pdo_count('jy_merch_member',array('fmerchid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'id');
		$v['orderprice']=pdo_getsum('jy_weishop_order',array('ispay'=>1,'merchid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'price');
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'多商户管理'),
		),
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'tip'=>'[<red>温馨提示</red>]:【会员总数】：这里是多商户商城锁定会员的总数量；【订单金额】：这个是多商户锁定得会员去别的商户消费的总金额；','list'=>array(
			array('title'=>'ID/MID','type'=>'img_string',
			'data'=>array(
				array('title'=>'ID：','name'=>'id'),
				array('title'=>'MID：','name'=>'mid')
				)
			),
			array('title'=>'商户信息_Merch','type'=>'img_string','img'=>'logo',
			'data'=>array(
				array('title'=>'名称：','name'=>'merchname'),
				array('title'=>'项目：','name'=>'salecate')
				)
			),
			array('name'=>'groupid','title'=>'商户等级','type'=>'select','options'=>'group','option_title'=>'groupname'),
			array('name'=>'url','title'=>'入口','type'=>'ewm','action'=>'appurl'),	
			array('title'=>'分类/时间','type'=>'img_string',
			'data'=>array(
				array('title'=>'分类：','name'=>'catename'),
				array('title'=>'入驻：','name'=>'jointime')
				)
			),
			array('name'=>'membernum','title'=>'会员总数','type'=>'string'),
			array('name'=>'orderprice','title'=>'订单金额','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status2'),
		),'action'=>array(
			array('type'=>'modal','op'=>'look','title'=>'商户信息'),
			array('type'=>'modal','op'=>'setgroup','title'=>'设置商户等级'),
			array('type'=>'href','op'=>'member','title'=>'商户会员'),
		)),
		'search'=>array(
			array('name'=>'merchname','width'=>8,'title'=>'商户名称','type'=>'string'),
			array('name'=>'realname','width'=>8,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>8,'title'=>'电话','type'=>'string'),
			array('name'=>'status','width'=>8,'title'=>'状态','type'=>'radio','options'=>'status2'),
			array('type'=>'pcd2','width'=>'40','title'=>'区域','name'=>'pcd','help'=>''),
		)
	);
	$return['status2']=$this->merch_status;
	$return['group']=pdo_getall('jy_merch_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_merch_user',array('id'=>$id));
		$item['lat_lng']=json_decode($item['lat_lng']);
		$item['onlinetime']=json_decode($item['onlinetime'],true);
	}else{
		$item=array();
	}
	$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	$item['upass']='';
	$item['diyformdata']=$item['diyformdata']?json_decode($item['diyformdata'],true):array();
	$formtypes=get_form_types();	
	foreach($item['diyformdata'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		//$v['type']=$formtypes[$v['type']];
	}
    $return['item']=$item;
	$htmls=array( 
		'type'=>'look',
		'desc'=>'商户详情',
		'look'=>array(
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'logo','title'=>'商户logo','type'=>'image'),
			array('type'=>'image','title'=>'店招','name'=>'topimg'),
			array('name'=>'isbrand','title'=>'属于品牌？','type'=>'switch'),
			array('name'=>'salecate','title'=>'主营项目','type'=>'string'),	
			array('name'=>'cateid','title'=>'商户分类','type'=>'select','options'=>'cate','option_title'=>'catename'),
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'desc','title'=>'商户简介','type'=>'text'),
			array('type'=>'text','title'=>'商户公告','name'=>'remark'),
			array('name'=>'address','title'=>'商户地址','type'=>'string'),	
			array('name'=>'tel','title'=>'商户电话','type'=>'string'),
			array('name'=>'groupid','title'=>'商户组','type'=>'select','options'=>'group','option_title'=>'groupname'),	
			array('name'=>'codeimg','title'=>'营业执照/许可证','type'=>'images'),
			array('name'=>'content','title'=>'商户详情','type'=>'richtext'),
			array(
						'title'=>'其他信息',
						'type'=>'array',
						'name'=>'diyformdata',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			)
		)
	);
	$return['cate']=pdo_getall('jy_merch_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	$return['group']=pdo_getall('jy_merch_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setgroup'){
	if($_RQ['postdata']){
		$post=get_postdata();
		pdo_update('jy_merch_user',array('groupid'=>$post['groupid']),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
        model('jy_merch/model')->upmember($post['groupid'],$post['mid']);
		exi('修改成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_merch_user',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','mid','groupid'));
	if(!$item){
		exi('指定商户不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改商户等级',
		'edit'=>array(
			array('name'=>'groupid','title'=>'商户等级','type'=>'select','options'=>'group','option_title'=>'groupname'),	
		),
	);
	$return['group']=pdo_getall('jy_merch_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'),'id');
	exi($return);
}
if($_RQ['op']=='member'){
	$id=intval($_RQ['id'])?:$this->session('merchid');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($id){
		$params['merchid']=$id;
		$return['params']['id']=$id;
		$this->session('merchid',$id);
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=pdo_getlist('jy_merch_member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['merchidtitle']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
			$item['fmerchidtitle']=pdo_getcolumn('jy_merch_user',array('id'=>$item['fmerchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'merchidtitle','title'=>'绑定商户','type'=>'string'),
			array('name'=>'fmerchidtitle','title'=>'来源商户','type'=>'string'),
			array('name'=>'orderprice','title'=>'自购金额','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'setfmerchid','title'=>'设置来源商户'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'会员id'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setfmerchid'){
	if($_RQ['postdata']){
		$post=get_postdata();
		pdo_update('jy_merch_member',array('fmerchid'=>$post['fmerchid']),array('fmerchid >'=>0,'mid'=>$post['mid'],'uniacid'=>$_SESSION['uniacid']));
		exi('修改成功！','','goback');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_merch_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('指定用户不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改来源商户',
		'edit'=>array(
			array('type'=>'choose','action'=>'jy_merch/get_merch/','name'=>'fmerchid','title'=>'来源商户id'),
		),
	);
	$return['group']=pdo_getall('jy_merch_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'),'id');
	exi($return);
}
?>